$(function () {
    //获取各大主流浏览器版本号等相关信息
    (function () {
        function getBroswer() {
            //定义浏览器对象
            var Sys = {};
            //获取浏览器相关信息
            var ua = navigator.userAgent.toLowerCase();
            //定义保存临时浏览器信息得变量
            var broswer;
            //正则匹配浏览器版本号
            (broswer = ua.match(/edge\/([\d.]+)/)) ? Sys.edge = broswer[1] :
                (broswer = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = broswer[1] :
                    (broswer = ua.match(/msie ([\d.]+)/)) ? Sys.ie = broswer[1] :
                        (broswer = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = broswer[1] :
                            (broswer = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = broswer[1] :
                                (broswer = ua.match(/opera.([\d.]+)/)) ? Sys.opera = broswer[1] :
                                    (broswer = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = broswer[1] : 0;

            //返回浏览器名称和浏览器版本号
            if (Sys.edge) return {broswer: "Edge", version: Sys.edge};
            if (Sys.ie) return {broswer: "IE", version: Sys.ie};
            if (Sys.firefox) return {broswer: "Firefox", version: Sys.firefox};
            if (Sys.chrome) return {broswer: "Chrome", version: Sys.chrome};
            if (Sys.opera) return {broswer: "Opera", version: Sys.opera};
            if (Sys.safari) return {broswer: "Safari", version: Sys.safari};

            return {broswer: "", version: "0"};
        }

        //获取浏览器对象
        var Broswer = getBroswer();
        console.log("broswer:" + Broswer.broswer + " version:" + Broswer.version);
        if (Broswer.broswer == 'IE' && Broswer.version <= 9) {
            $('.low-version').css('display', 'block');
        }
    }());

    $.ajaxSetup({
        cache: true
    });
        //定义全局获取地址栏参数函数
        window.getUrlParams = function (url) {
        //获取url地址栏的哈希值
        var hash = url ? '#' + url.split('#')[1] : decodeURIComponent(location.hash);
        //通过'?'分割得到的哈希值
        var hashDeatail = hash.split("?"),
            hashName = hashDeatail[0].split("#")[1], //路由地址
            params = hashDeatail[1] ? (hashDeatail[1].split("&") ? hashDeatail[1].split("&") : hashDeatail[1]) : [], //参数内容
            query = {};
        for (var i = 0; i < params.length; i++) {
            var item = params[i].split("=");
            query[item[0]] = item[1]
        }
        return {
            path: hashName,//栏目名
            query: query, //查询字符串对象
            params: params //未进行提取的原参数数组
        }
    }
         //定义全局加载栏目内容的函数
        window.loadInner = function(path, obj) {
        // var path = path ? path : decodeURI(window.location.hash);
        var obj = $.type(obj) != "object" || $.isEmptyObject(obj) ? getUrlParams().query : obj;
        //var obj = getUrlParams().query;
        var path = path || '01';
        var page = './api/getPages.php';
        obj.cateNo = path;
        $("#load-css").load(page + ' #css-file', obj);
        //加载相对应的内容
        $("#content").load(page + ' .container', obj, function (res, status, xhr) {
            $("#load-script").load(page + " #script-file", obj, function (res) {
                // $("#load-script").empty();
                var scripts = $(res).find('script');
                scripts.each(function (index, item) {
                    $.getScript($(item).attr("data-src"));
                });
            });
        });
    }
        
        //创建导航条滑动对象
        var navbarSwiper = new Swiper('.navbar-mmvtc .swiper-container', {});
        //获取栏目总数
        var categoryCount = $('.navbar-nav > li').length;

        // 监听窗口大小变化
        function resize() {
            // 屏幕宽度
            var screenWidth = window.innerWidth;
            // 是否为小于860的屏幕
            var smallScreen = screenWidth <= 860;
            if (smallScreen) {
                $(".top .navbar-nav").off("mouseover mouseout");
                $("#btn-search").off("click");
                //给搜索按钮注册点击事件
                $("#btn-search").click(function () {
                    var searchBox = $(".top .sm-searchbox");
                    if (searchBox.is(":hidden")) {
                        $("#btn-search").toggleClass("glyphicon-search glyphicon-remove");
                        $(".top .sm-searchbox .input-search").focus();
                        searchBox.slideDown();
                    } else {
                        searchBox.slideUp();
                        $("#btn-search").toggleClass("glyphicon-search glyphicon-remove");
                    }
                });
                //销毁顶部导航条滑动对象
                if (navbarSwiper.initialized != undefined) navbarSwiper.destroy(true);
                //创建垂直方向的导航条对象
                navbarSwiper = new Swiper('.navbar-mmvtc .swiper-container', {
                    direction: 'vertical',
                    zoom: true,
                    slidesPerView: categoryCount,
                    roundLengths: true,
                    freeMode: true,
                    height: window.innerHeight,
                    noSwiping: true,
                    // virtualTranslate : true,

                });
                navbarSwiper.$wrapperEl.css('height');
                $('.navbar-mmvtc .swiper-slide').addClass('swiper-no-swiping');
                // document.querySelector('.navbar-collapse.collapse').style.display = null;
                var li = $('.top .navbar-collapse .navbar-nav > li');
                li.each(function (i, item) {
                    // $(item).removeClass();
                    $(item).css('height', '40px');
                });
            } else {
                //大屏幕隐藏小屏时的搜索框
                $('div.sm-searchbox:visible').css('display', 'none');
                $('#btn-search').addClass("glyphicon-search").removeClass('glyphicon-remove');
                //下拉菜单鼠标进入与移出
                $(".top .navbar-nav").on("mouseover mouseout", "li.swiper-slide", function (event) {
                    if (event.type == "mouseover") {
                        // $(this).children("a").addClass(':hover');
                        $(this).children("ul").stop().slideDown(500);
                    } else if (event.type == "mouseout") {
                        $(this).children("ul").stop().slideUp(500);
                    }
                });
                //导航条显示方式
                // document.querySelector(".top .row").style.display = "block";
                $('.navbar-mmvtc .swiper-slide').removeClass('swiper-no-swiping');
                //销毁顶部导航条滑动对象
                if (navbarSwiper.initialized != undefined) navbarSwiper.destroy(true);
                //创建水平方向的顶部导航条对象
                navbarSwiper = new Swiper('.navbar-mmvtc .swiper-container', {
                    direction: 'horizontal',
                    zoom: true,
                    slidesPerView: categoryCount,
                    roundLengths: true,
                    freeMode: true,
                    noSwiping: false,
                    pagination: {
                        el: '.navbar-mmvtc .swiper-pagination',
                        type: 'progressbar',
                        clickable: true,
                    },
                    navigation: {
                        nextEl: '.navbar-mmvtc .right-more',
                        prevEl: '.navbar-mmvtc .swiper-button-prev',

                    },
                    breakpoints: {
                        1200: {
                            slidesPerView: categoryCount > 10 ? categoryCount - 2 : categoryCount,

                        },
                        1090: {
                            slidesPerView: categoryCount > 10 ? categoryCount - 3 : categoryCount,
                        },
                        997: {
                            slidesPerView: categoryCount > 10 ? categoryCount - 4 : (categoryCount > 8 ? categoryCount - 2 : categoryCount),
                        },
                        880: {
                            slidesPerView: categoryCount > 10 ? categoryCount - 5 : (categoryCount > 8 ? categoryCount - 3 : categoryCount),
                        }
                    }
                });
            }
        }
        $(window).on('resize', resize).trigger('resize');



    //滚动之前的高度
    var beforeScrollTop = $(window).scrollTop();
    //滚动前的滚动方向
    var beforeScrollDirection = 'top';
    //监听页面滚动
    $(window).scroll(function () {
        //滚动后的滚动方向
        var afterScrollDirection = 'top';
        // 滚动之后的高度
        var afterScrollTop = $(window).scrollTop();
        //页面总高度
        var docHeight = $(document).height();
        //窗口总高度
        var winHeight = $(window).height();
        //获取屏幕宽度
        var screenWidth = $(window).width();
        if (screenWidth > 860) {
            //如果屏幕宽度大于860px,则显示移动端web二维码
            $(".assist-float-right .glyphicon-phone").show();
            //大屏时，如果垂直滚动距离大于300px,则显示右下角悬浮栏，否则隐藏
            if (afterScrollTop > 250) {
                //比较滚动前后距离页面顶部的距离 || 页面是否到达底部
                if (beforeScrollTop > afterScrollTop || winHeight + afterScrollTop +8 >= docHeight) {
                    //如果滚动前的距离大于滚动后的距离，则记录滚动后的方向为top
                    afterScrollDirection = 'top';
                } else {
                    //如果滚动前的距离大于滚动后的距离，则记录滚动后的方向为bottom
                    afterScrollDirection = 'bottom';
                }
                $('.assist-float-right').fadeIn();
                //判断滚动前后的方向是否一致
                if (beforeScrollDirection != afterScrollDirection) {
                    //如果滚动的方向为top，则将图标顺时针向下旋转180度
                    if (afterScrollDirection == 'top') {
                        $(".assist-float-right .icon-back-top").rotate({
                            angle: 180,
                            animateTo: 360, duration: 1000
                        });
                        //如果滚动的方向为bottom，则将图标顺时针向上旋转180度
                    } else if (afterScrollDirection == 'bottom') {
                        $(".assist-float-right .icon-back-top").rotate({
                            angle: 0,
                            animateTo: 180, duration: 1000
                        });
                    }

                }
                //将滚动后的滚动方向记录为前一次
                beforeScrollDirection = afterScrollDirection;
                //将滚动后的距离保存起来
                beforeScrollTop = afterScrollTop;
            } else {
                $('.assist-float-right').fadeOut();
                beforeScrollTop = afterScrollTop;
            }
        } else {
            //如果屏幕宽度小于860px,则隐藏移动端web二维码
            $(".assist-float-right .glyphicon-phone").hide();
            //小屏时，如果垂直滚动距离大于300px并且为向上滚动,则显示右下角悬浮栏，否则隐藏
            if (afterScrollTop > 300 && beforeScrollTop > afterScrollTop) {
                $('.assist-float-right').fadeIn();
                //如果鼠标向上滚动，则显示回到顶部按钮
                // if (beforeScrollTop > afterScrollTop) {
                //     $(".assist-float-right .icon-back-top").slideDown();
                // } else {
                //     $(".assist-float-right .icon-back-top").fadeOut();
                // }
                beforeScrollTop = afterScrollTop;
            } else {
                $('.assist-float-right').fadeOut();
                beforeScrollTop = afterScrollTop;
            }
        }

    }).trigger('scroll');
    //右下角悬浮栏 手机悬浮显示移动web版系部网站二维码
    $(".assist-float-right .glyphicon-phone").hover(
        function () {
            $(".assist-float-right .surf-mobile-web").animate({
                opacity: '1',
                right: '70px'
            });
        },
        function () {
            $(".assist-float-right .surf-mobile-web").animate({
                right: '0',
                opacity: '0'
            });
        });
    //右下角悬浮栏 手机悬浮显示微信公众号二维码
    $(".assist-float-right .icon-weixin").hover(
        function () {
            var screenWidth = $(window).width();
            if (screenWidth > 860){
                $(".assist-float-right .add-zhao-weixin").animate({
                    opacity: '1',
                    right: '70px'
                });
            } else {
                $(".assist-float-right .add-zhao-weixin").animate({
                    opacity: '1',
                    top: '85%',
                    right: '70px'
                });
            }
            $(".assist-float-right .add-apartment-weixin").animate({
                opacity: '1',
                top: '-123px'
            });
        },
        function () {
            $(".assist-float-right .add-apartment-weixin").animate({
                opacity: '0',
                top: '0'
            });
            $(".assist-float-right .add-zhao-weixin").animate({
                opacity: '0',
                right: '0'
            });
        });
    //回到顶部按钮注册点击事件
    $(".assist-float-right .icon-back-top").click(function () {
        //获取回到顶部按钮的角度
        var rotateAngle = $(this).getRotateAngle();
        var scrollWidth = $(window).width();
        //如果回到顶部按钮的角度小于等于180度，则点击按钮后到达页面底部，否则回到顶部
        if (rotateAngle <= 180 && scrollWidth > 860) {
            $('html,body').animate({scrollTop: $('html,body').height() + 'px'}, 600);
            $(".assist-float-right .icon-back-top").rotate({
                angle: 180,
                animateTo: 360, duration: 1000
            });
        }else {
            $('html,body').animate({scrollTop: '0px'}, 600);
        }
    });
});